﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace System.Windows.JControls.JColorPicker.Tracker
{
    /// <summary>
    /// A Tracker for the hue value
    /// </summary>
    [System.ComponentModel.EditorBrowsable( System.ComponentModel.EditorBrowsableState.Never)]
    class HSBHueTracker : ColorTracker
    {
        /// <summary>
        /// Initialze the object.
        /// </summary>
        public HSBHueTracker()
            : base()
        {
            SetValue(0, 360, 0);
        }

        /// <summary>
        /// Set the value from the hue from the mousepoint.
        /// </summary>
        /// <param name="mousePoint"></param>
        private void SetColorValue(Point mousePoint)
        {
            Rectangle rect = RectangleUtils.RectanglePaint(ClientRectangle, Padding);
            double selectValue = 0;

            // X from mousepoint is < as the x from the rect
            if (mousePoint.X < rect.X)
            {
                selectValue = 0;
            }
                // x from the mousepoint is > as the right side from the rectangle.
            else if (mousePoint.X > rect.Right)
            {
                selectValue = MaxValue;
            }
                // get the value
            else
            {
                selectValue = ((double)(mousePoint.X - rect.Left)) * MaxValue / (double)rect.Width;
            }

            // should the value > as the maxvalue
            if (selectValue > MaxValue)
            {
                selectValue = MaxValue;
            }

            Value = selectValue;
        }

        #region Override Methods

        /// <summary>
        /// Paint the Tracker
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            RectangleF rect = RectangleUtils.ConvertToRectangleF(RectangleUtils.RectanglePaint(ClientRectangle, Padding));

            // colorpath through all colors
            Color[] colors = new Color[]{Color.FromArgb(255,0,0),
                                               Color.FromArgb(255,255,0),
                                               Color.FromArgb(0,255,0),
                                               Color.FromArgb(0,255,255),
                                               Color.FromArgb(0,0,255),
                                               Color.FromArgb(255,0,255),
                                               Color.FromArgb(255,0,0)};

            // the steps for painting the linear color
            float widthStep = rect.Width / (colors.Length - 1);
            int index = 0;

            RectangleF rectpaint = new RectangleF(rect.X, rect.Y, widthStep, rect.Height);
            do
            {
                using (LinearGradientBrush brush = new LinearGradientBrush(rectpaint, colors[index], colors[index + 1], LinearGradientMode.Horizontal))
                {
                    e.Graphics.FillRectangle(brush, rectpaint);
                }

                rectpaint.X += widthStep;
                index++;

            } while (index < colors.Length - 1);

            // Paint selector
            PaintSelectors(e.Graphics, Rectangle.Ceiling(rect));
        }

        /// <summary>
        /// paint the selecotor
        /// </summary>
        /// <param name="gr"></param>
        /// <param name="rectPaint"></param>
        private void PaintSelectors(Graphics gr, Rectangle rectPaint)
        {
            GraphicsPath grpathUp = GetSignUpPath(rectPaint);

            using (SolidBrush brush = new SolidBrush(Color.Bisque))
            {
                gr.FillPath(brush, grpathUp);
            }

            using (Pen pen = new Pen(Color.Black))
            {
                gr.DrawPath(pen, grpathUp);
            }
        }

        /// <summary>
        /// <b>Overriden:</b> When the user click the left mouse button.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                SetColorValue(e.Location);
            }
        }

        /// <summary>
        /// <b>Overriden:</b> When the user move the mouse and is hold down the left mouse button.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                SetColorValue(e.Location);
            }
        }

        #endregion
    }
}
